package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Menu;
import com.kkb.pojo.ResultVO;
import com.kkb.pojo.Role;
import com.kkb.service.MenuService;
import com.kkb.service.RoleService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("role")
@ResponseBody
public class RoleController {
    @Resource
    private RoleService roleService;
    @Resource
    private MenuService menuService;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ResultVO<Role> queryByPage(Integer pageNum, Integer pageSize, String roleName) {
        if (pageNum == null || pageNum <= 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize <= 0) {
            pageSize = 5;
        }
        PageInfo<Role> info = roleService.queryByPage(pageNum, pageSize, roleName);
        return new ResultVO<>(info);
    }
    @RequestMapping(value = "menuList", method = RequestMethod.GET)
    public ResultVO<Menu> queryMenuList() {
        List<Menu> menuList = menuService.queryMenuList();
        return new ResultVO<>(menuList);
    }

    /**
     * 重复性验证
     * @param roleName
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResultVO<Role> queryByRoleName(String roleName) {
        List<Role> roleList = roleService.queryByRoleName(roleName);
        if(roleList.size()>0){
            return new ResultVO<>(500);
        }else {
            return new ResultVO<>(200);
        }
    }

    /**
     * 添加角色
     * @param role
     * @param menuIds
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    public ResultVO<Role> addRole(Role role,@RequestParam("group[]") List<Integer> menuIds) {
        int num = roleService.addRole(role, menuIds);
        if (num == 1+menuIds.size()) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "添加失败");
        }
    }

    /**
     * 三表联查
     * @param roleId
     * @return
     */
    @RequestMapping(value = "{id}", method = RequestMethod.GET)
    public ResultVO<Role> queryById(@PathVariable("id") Integer roleId) {
        //System.out.println("roleId"+roleId);
        Role role = roleService.queryById(roleId);
        if(role!=null){
            return new ResultVO<>(role);
        }
        Role role1 = roleService.queryByRoleId(roleId);
        return new ResultVO<>(role1);
    }

    @RequestMapping(value = "", method = RequestMethod.PUT)
    public ResultVO<Role> updateRole(Role role,@RequestParam("group[]") List<Integer> menuIds) {
        int i = roleService.updateRole(role, menuIds);
        if (i == 1+menuIds.size()) {
            return new ResultVO<>(200);
        }else if (i == -1) {
            return new ResultVO<>(500, "角色名重复");
        } else {
            return new ResultVO<>(500, "更新失败");
        }
    }
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public ResultVO<Role> deleteRole(@PathVariable("id") Integer roleId) {
        int i = roleService.deleteRole(roleId);
        if (i == 1) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }
    @RequestMapping(value = "", method = RequestMethod.DELETE)
    public ResultVO<Role> deleteRoles(@RequestParam("ids[]") List<Integer> ids) {
      /*  for (Integer id : ids) {
            System.out.println(id);
        }*/
       int i = roleService.deleteRoles(ids);
        if (i == ids.size()) {
            return new ResultVO<>(200);
        } else {
            return new ResultVO<>(500, "删除失败");
        }
    }

}
